//
//  WeatherView.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/17.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "WeatherView.h"
#import "WeatherModel.h"
#import "WeatherTableViewCell.h"


@interface WeatherView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UILabel *cityLabel;

@property (nonatomic, retain) UILabel *dateLabel;

@property (nonatomic, retain) UILabel *weatherLbale;

@property (nonatomic, retain) UILabel *tempLabel;

@property (nonatomic, retain) NSMutableArray *modelArray;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSString *weaid;

@end

@implementation WeatherView

-(instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self hanldeData];
        
        self.modelArray = [[NSMutableArray alloc] init];
        
        self.cityLabel = [[UILabel alloc] init];
        [self addSubview:_cityLabel];
        
        self.dateLabel = [[UILabel alloc] init];
        [self addSubview:_dateLabel];
        
        self.weatherLbale = [[UILabel alloc] init];
        [self addSubview:_weatherLbale];
        
        self.tempLabel = [[UILabel alloc] init];
        [self addSubview:_tempLabel];
        
        
    }
    return self;
    
}

-(void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height * 0.35, self.bounds.size.width, 300) style:UITableViewStylePlain];
    [self addSubview:_tableView];
    
    self.tableView.scrollEnabled = NO;
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[WeatherTableViewCell class] forCellReuseIdentifier:@"weatherCell"];
    
}

-(void)hanldeData {
    
    if (!self.cityid) {
        self.weaid = @"446";
    } else {
        self.weaid = self.cityid;
    }
    
    NSString *str = [NSString stringWithFormat:@"http://api.k780.com:88/?app=weather.future&weather.future&weaid=%@&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json", self.weaid];
    AFHTTPSessionManager *man = [AFHTTPSessionManager manager];
    
    [man GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray *resultArr = [responseObject objectForKey:@"result"];
        
        for (NSDictionary *dic in resultArr) {
            
            WeatherModel *model = [[WeatherModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.modelArray addObject:model];
            
            
        }
        
        [self config];
        [self createTableView];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@", error);
        
    }];
    
}

-(void)config {
    
    [self.cityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self).with.offset(60);
        
        make.left.equalTo(self).with.offset(20);
        
        make.width.offset(self.bounds.size.width / 2 - 20);
        
        make.height.offset(50);
        
    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.cityLabel).with.offset(60);
        
        make.left.equalTo(self).with.offset(20);
        
        make.width.offset(self.bounds.size.width / 2 - 20);
        
        make.height.offset(25);
        
    }];
    
    [self.weatherLbale mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self.dateLabel).with.offset(35);
        
        make.left.equalTo(self).with.offset(20);
        
        make.width.offset(self.bounds.size.width / 2 - 20);
        
        make.height.offset(25);
        
    }];
    
    [self.tempLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(self).with.offset(80);
        
        make.left.equalTo(self).with.offset(self.bounds.size.width / 2);
        
        make.width.offset(self.bounds.size.width / 2 );
        
        make.height.offset(120);
        
    }]; 
    
    self.cityLabel.textColor = [UIColor whiteColor];
    self.cityLabel.font = [UIFont systemFontOfSize:30];
    self.dateLabel.textColor = [UIColor whiteColor];
    self.dateLabel.font = [UIFont systemFontOfSize:15];
    self.weatherLbale.textColor = [UIColor whiteColor];
    self.tempLabel.textColor = [UIColor whiteColor];
    self.tempLabel.font = [UIFont systemFontOfSize:48];
    self.tempLabel.textAlignment = NSTextAlignmentCenter;
    
    WeatherModel *model = [self.modelArray objectAtIndex:0];
    
    self.cityLabel.text = model.citynm;
    
    NSString *date = [NSString stringWithFormat:@"%@/%@", model.days, model.week];
    
    self.dateLabel.text = date;
    
    self.weatherLbale.text = model.weather;
    
    NSString *temp = [NSString stringWithFormat:@"%@℃", model.temp_high];
    
    self.tempLabel.text = temp;
    
    if ([model.weather isEqualToString:@"晴"]) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"晴天"]];
    } else if ([model.weather isEqualToString:@"多云"]) {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"阴天"]];
    } else {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"雨天"]];
    }
    
}



- (UINavigationController*)naviController {
    
    for (UIView* next = [self superview]; next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController*)nextResponder;
        }
    }
    
    return nil;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 6;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WeatherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"weatherCell"];
    
    WeatherModel *model = [self.modelArray objectAtIndex:indexPath.row];
    
    cell.weatherModel = model;
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
    
}

-(void)setCityid:(NSString *)cityid {
    
    if (_cityid != cityid) {
        _cityid = cityid;
    }
    
    [self hanldeData];
    [self.tableView reloadData];
    NSLog(@"8741328794458418455612316878947897897897897897897897897897897897897897798%@", cityid);
    
}

@end
